from flask_migrate import init, migrate, upgrade
from app import db
from app.models import Project, TestCase, TestResult, Report, DataSource
from app import create_app
import os

def init_db():
    """初始化数据库"""
    app = create_app()
    
    with app.app_context():
        # 创建所有表
        db.create_all()
        
        # 创建一个示例项目
        if not Project.query.first():
            project = Project(
                name="示例项目",
                description="这是一个示例项目，用于演示系统功能"
            )
            project.save()
            
            # 创建示例数据源
            data_source = DataSource(
                name="示例数据源",
                project_id=project.id,
                type="json",
                config='{"url": "https://api.example.com/data"}',
                description="示例API数据源"
            )
            data_source.save()
            
            # 创建示例测试用例
            test_case = TestCase(
                name="示例测试用例",
                project_id=project.id,
                method="GET",
                url="https://jsonplaceholder.typicode.com/posts/1",
                headers='{"Content-Type": "application/json"}'
            )
            test_case.save()
            
            print("数据库初始化完成！")
        else:
            print("数据库已存在数据，跳过初始化。")

if __name__ == '__main__':
    init_db()